package com.jketing.tag;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import com.jketing.Config;

/**
 * UrlTag 换肤资源定位处理类
 * 
 * 使用SpringData的Page封装分页数据
 * 
 * @author  李荣华
 * @version v1.0
 * @date        2017-10-31
 * @Servlet   3.1
 * @Jsp-api   2.3.1
 */
@SuppressWarnings("serial")
public class UrlTag extends TagSupport {
	public String css;
	public String image;
	public String icon;
	public String js;
	
	/**
	 * 		defaultTheme 系统默认皮肤
	 */
	private static final String defaultTheme = Config.getPropertyByKey("default.theme");
	
	
	public int doStartTag() throws JspException{	
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		JspWriter out = pageContext.getOut();
		String outPath = request.getContextPath();		
		
		//处理   css image  icon
		try {
			//获取当然登录用户session内皮肤主题
			String themesName = (String)request.getSession().getAttribute("themesName");
			String webPath = "";
			if(themesName != null && !"".equals(themesName))
				webPath += "/Themes/"+themesName+"/";
			else
				webPath += "/Themes/"+defaultTheme+"/";
			
			
			String path = null;
			if(css != null){
				path ="css/"+css;
			}else if(image != null){
				path ="image/"+image;
			}else if(icon != null){
				path ="icon/"+icon;
			}else if(js != null){
				path ="js/"+js;
			}
			
			//检查文件是否存在，不存在则设置为默认风格目录
			if(path != null){
				String filePath = pageContext.getServletContext().getRealPath(webPath+path);
				File file = new File(filePath);
				if(!file.exists()){
					outPath += "/Themes/"+defaultTheme+"/"+path;
				}else
					outPath += webPath+path;
//				System.out.println(filePath+"   "+file.exists() +"  outPath:"+outPath);
			}
			out.print(outPath);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new JspException("Netware Fail.");
		}
		return Tag.SKIP_BODY;
	}

	public String getCss() {
		return css;
	}
	public void setCss(String css) {
		this.css = css;
	}

	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}

	public String getIcon() {
		return icon;
	}
	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getJs() {
		return js;
	}
	public void setJs(String js) {
		this.js = js;
	}
}
